﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using Module.DataAccess;
using Module.EntitiesInfo;


namespace Module.BusinessLogic
{
    public class AnswerBL
    {
        public static DataTable GetAllByQuestionId(int questionId)
        {
            try
            {
                return new AnswerDB().GetAllByQuestionId(questionId);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static DataTable GetAnswerUnCorrect(int top, int questionId, int bitMaskCorrect)
        {
            try
            {
                return new AnswerDB().GetAnswerUnCorrect(top, questionId, bitMaskCorrect);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void Delete(int type, int id)
        {
            try
            {
                new AnswerDB().Delete(type, id);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int Insert(AnswerInfo info)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
                return new AnswerDB().Insert(info);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void Update(AnswerInfo info)
        {
            try
            {
                new AnswerDB().Update(info);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void UpdateStatus(int type, int id, int values)
        {
            try
            {
                new AnswerDB().UpdateStatus(type, id, values);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static AnswerInfo GetInfo(int id)
        {
            try
            {
                AnswerInfo answerInfo;
                answerInfo = ObjectHelper.FillObject<AnswerInfo>(new AnswerDB().GetInfo(id));
                if (answerInfo != null)
                    return answerInfo;
                return null;                
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static List<AnswerInfo> GetAllByQId(int questionId)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                string strCache = questionId + "{50F052BB-386A-4E7B-84B5-7C8ACC756029}";
                List<AnswerInfo> answerInfo = new List<AnswerInfo>();
                if (cache.GetCache(strCache) != null)
                {
                    answerInfo = (List<AnswerInfo>)(cache.GetCache(strCache));
                    return answerInfo;
                }
                answerInfo = ObjectHelper.FillCollection<AnswerInfo>(new AnswerDB().GetAllByQId(questionId));
                if (answerInfo != null && answerInfo.Count > 0)
                {
                    cache.InsertCache(strCache, answerInfo, Convert.ToDouble(ConfigurationSettings.AppSettings["timeCache"].ToString()));
                }
               return answerInfo;                                        
                
            }
            catch (Exception)
            {
                throw;
            }
        }


        public static List<AnswerInfo> GetUncorrect(int top, int questionId)
        {
            try
            {
                List<AnswerInfo> answerInfo;
                answerInfo = ObjectHelper.FillCollection<AnswerInfo>(new AnswerDB().GetUncorrect(top, questionId));
                if (answerInfo != null)
                    return answerInfo;
                return null;                 
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

